﻿using System;

using Spencen.Mobile.Markup;
using Spencen.Mobile.UI;
using Spencen.Mobile.UI.Commanding;
using Spencen.Mobile.UI.Controls;
using Spencen.Mobile.UI.Easings;
using Spencen.Mobile.UI.Layout;
using Spencen.Mobile.UI.Primitives;
using Spencen.Mobile.UI.Templating;
using Spencen.Mobile.UI.Navigation;

namespace Demo.Presenters
{
    public class LayoutPresenter : ParentedPresenter
    {
        private DrawingContainer _layoutContainer;
        private LayoutType _layoutType;

        public LayoutPresenter( INavigator navigator ) : base ( navigator )
        {
            Items = new ObservableList();
            AddItemCommand = new DelegateCommand( AddItem );
            RemoveItemCommand = new DelegateCommand( RemoveItem );
            ToggleLayoutCommand = new DelegateCommand( ToggleLayout );
        }

        public ICommand AddItemCommand { get; private set; }
        public ICommand RemoveItemCommand { get; private set; }
        public ICommand ToggleLayoutCommand { get; private set; }
        public ObservableList Items { get; private set; }

        public override View CreateView( IDrawingHost host )
        {
            // XAML Loading
            var text = (System.IO.Stream)System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream( "Demo.Views.LayoutView.xml" );
            var xamlRenderer = new XamlRenderer( host, this );
            var view = (View)xamlRenderer.Render( text );

            _layoutContainer = (DrawingContainer)xamlRenderer.FindResource( "layoutContainer" );

            return view;
        }

        private void AddItem()
        {
            var newItem = new DemoItem() { Text = ( Items.Count + 1 ).ToString() };
            Items.Add( newItem );
        }

        private void RemoveItem()
        {
            if ( Items.Count > 0 )
                Items.RemoveAt( Items.Count - 1 ); // Remove the last item
        }

        private void ToggleLayout()
        {
            _layoutType = ( LayoutType ) ( ( ( (int) _layoutType ) + 1 ) % 3 );

            switch ( _layoutType )
            {
                case LayoutType.Stack:
                    _layoutContainer.LayoutEngine = new StackLayout()
                    {
                        AnimateMovements = true,
                        AnimationDuration = new TimeSpan( 0, 0, 1 ),
                        AnimationEasing = new ElasticEasing(),
                        Padding = new Padding( 4 ),
                        Margin = new Padding( 4 )
                    };
                    break;

                case LayoutType.Radial:
                    _layoutContainer.LayoutEngine = new RadialLayout()
                    {
                        AnimateMovements = true,
                        AnimationDuration = new TimeSpan( 0, 0, 1 ),
                        AnimationEasing = new ElasticEasing()
                    };
                    break;

                case LayoutType.Wrap:
                    _layoutContainer.LayoutEngine = new WrapLayout()
                    {
                        AnimateMovements = true,
                        AnimationDuration = new TimeSpan( 0, 0, 1 ),
                        AnimationEasing = new ElasticEasing(),
                        Padding = new Padding( 4 ),
                        Margin = new Padding( 4 )
                    };
                    break;
            }
        }

        private enum LayoutType
        {
            Stack,
            Wrap,
            Radial
        }
    }

    public class DemoItem
    {
        public string Text { get; set; }
        public override string ToString()
        {
            return Text;
        }
    }

    public class DemoItemTemplate : IDataTemplate
    {
        #region IDataTemplate Members

        public DrawingElement ApplyTemplate( IDrawingHost host, object dataItem )
        {
            return new GlassButton()
            {
                DesiredSize = new DesiredSize( 80, 40 ),
                Text = ( (DemoItem)dataItem ).Text
            };
        }

        #endregion
    }
}
